/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ocp.util;

import org.knopflerfish.service.log.LogRef;
import org.knopflerfish.service.log.LogService;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;

/**
 * Utilidad para hacer log en OSGi
 * SE DEBE INICIALIZAR con el método init
 * @author Francisco
 */
public class Log {
    
    private static Log log;
    private static LogRef logref;

    public Log(BundleContext bc)  {
        logref = new LogRef(bc);
    }

    public static void init(BundleContext bc) {
        log = new Log(bc);
    }
    
    static public void logDebug( String message) {
        log( LogService.LOG_DEBUG , message );
    }
    
    static public void logInfo( String message) {
        log( LogService.LOG_INFO , message );
    }

    static public void logWarning( String message) {
        log( LogService.LOG_WARNING , message );
    }
    
    static public void logWarning( String message , Throwable exception) {
        log( LogService.LOG_WARNING , message , exception );
    }
    
    static public void logError( String message) {
        log( LogService.LOG_ERROR , message );
    }
    
    static public void logError( String message , Throwable exception) {
        log( LogService.LOG_ERROR , message , exception );
    }
    
    
    /**
     * Log a message. The ServiceDescription field and the Throwable
     * field of the LogEntry will be set to null.
     *
     * @param level The severity of the message. (Should be one of the
     *            four predefined severities.)
     * @param message Human readable string describing the condition.
     */
    static public void log(int level, String message) {
        if( level <= logref.getLogLevel() )
            logref.log( level, message );
    }

    /**
     * Log a message with an exception. The ServiceDescription field
     * of the LogEntry will be set to null.
     *
     * @param level The severity of the message. (Should be one of the
     *            four predefined severities.)
     * @param message Human readable string describing the
     *            condition.
     * @param exception The exception that reflects the condition.
     */
    static public void log(int level, String message, Throwable exception) {
        if( level <= logref.getLogLevel() )
            logref.log( level, message , exception );
    }

    /**
     * Log a message associated with a specific Service. The Throwable
     * field of the LogEntry will be set to null.
     *
     * @param sr The <code>ServiceReference</code> of the service that
     *            this message is associated with.
     * @param level The severity of the message. (Should be one of the
     *            four predefined severities.)
     * @param message Human readable string describing the condition.
     */
    static public void log(ServiceReference sr, int level, String message) {
        if( level <= logref.getLogLevel() )
            logref.log( sr , level, message );
    }

    /**
     * Log a message with an exception associated with a specific
     * Service.
     *
     * @param sr The <code>ServiceReference</code> of the service that
     *           this message is associated with.
     * @param level The severity of the message. (Should be one of the
     *            four predefined severities.)
     * @param message Human readable string describing the condition.
     * @param exception
     *            The exception that reflects the condition.
     */
    static public void log(ServiceReference sr, int level, String message,
            Throwable exception) {
        if( level <= logref.getLogLevel() )
            logref.log( sr , level, message , exception );
    }

}
